home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2793.asc < prev    next >
Text File  |  1996-09-15  |  8KB  |  366 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2793
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  May 17, 1995                             PAGE  :  1/6
  11.  
  12.     TITLE  :  Adding Graphics in Your Listboxes and Comboboxes
  13.  
  14.  
  15.  
  16.  
  17. TI:  Inserting graphics in Owner Drawn ListBoxes and ComboBoxes
  18.  
  19. The ability to place graphics inside ListBoxes and ComboBoxes 
  20. can improve the look of your application and set your user 
  21. interface apart from the others. 
  22.  
  23. Q: How do I stick graphics in a Listbox or ComboBox???
  24.  
  25. Here is an step-by-step example.....
  26.  
  27. 1.  Create a form.
  28.  
  29. 2.  Place a ComboBox and Listbox component on your form.
  30.  
  31. 3.  Change the Style property of the ComboBox component to 
  32. csOwnerDrawVariable and the Style property of the ListBox to 
  33. lbOwnerDrawVariable.
  34.  
  35. An Owner-Draw TListBox or TComboBox allows you to display 
  36. both objects (ex. graphics) and strings as the items.  For 
  37. this example, we are adding both a graphic object and a 
  38. string.
  39.  
  40. 4.  Create 5 variables of type TBitmap in the Form's VAR 
  41. section.
  42.  
  43. 5.  Create a Procedure for the Form's OnCreate event.
  44.  
  45. 6.  Create a Procedure for the ComboBox's OnDraw Event.
  46.  
  47. 7.  Create a Procedure for the ComboBox's OnMeasureItem.
  48.  
  49. 8. Free the resources in the Form's OnClose Event.
  50.  
  51.  
  52. {START OWNERDRW.PAS}
  53. unit Ownerdrw;
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2793
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  May 17, 1995                             PAGE  :  2/6
  72.  
  73.     TITLE  :  Adding Graphics in Your Listboxes and Comboboxes
  74.  
  75.  
  76.  
  77.  
  78. interface
  79.  
  80. uses
  81.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  82.   Forms, Dialogs, StdCtrls;
  83.  
  84. type
  85.   TForm1 = class(TForm)
  86.     ComboBox1: TComboBox;
  87.     ListBox1: TListBox;
  88.     procedure FormCreate(Sender: TObject);
  89.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  90.     procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  91.       Rect: TRect; State: TOwnerDrawState);
  92.     procedure ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  93.       var Height: Integer);
  94.     procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
  95.       Rect: TRect; State: TOwnerDrawState);
  96.     procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  97.       var Height: Integer);
  98.   private
  99.     { Private declarations }
  100.   public
  101.     { Public declarations }
  102.   end;
  103.  
  104. var
  105.   Form1: TForm1;
  106.   TheBitmap1, TheBitmap2, TheBitmap3, TheBitmap4,
  107.   TheBitmap5 : TBitmap;
  108. implementation
  109.  
  110. {$R *.DFM}
  111.  
  112. procedure TForm1.FormCreate(Sender: TObject);
  113. begin
  114.   TheBitmap1 := TBitmap.Create;
  115.   TheBitmap1.LoadFromFile('C:\delphi\images\buttons\globe.bmp');
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2793
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  May 17, 1995                             PAGE  :  3/6
  133.  
  134.     TITLE  :  Adding Graphics in Your Listboxes and Comboboxes
  135.  
  136.  
  137.  
  138.  
  139.   TheBitmap2 := TBitmap.Create;
  140.   TheBitmap2.LoadFromFile('C:\delphi\images\buttons\video.bmp');
  141.   TheBitmap3 := TBitmap.Create;
  142.   TheBitmap3.LoadFromFile('C:\delphi\images\buttons\gears.bmp');
  143.   TheBitmap4 := TBitmap.Create;
  144.   TheBitmap4.LoadFromFile('C:\delphi\images\buttons\key.bmp');
  145.   TheBitmap5 := TBitmap.Create;
  146.   TheBitmap5.LoadFromFile('C:\delphi\images\buttons\tools.bmp');
  147.   ComboBox1.Items.AddObject('Bitmap1: Globe', TheBitmap1);
  148.   ComboBox1.Items.AddObject('Bitmap2: Video', TheBitmap2);
  149.   ComboBox1.Items.AddObject('Bitmap3: Gears', TheBitmap3);
  150.   ComboBox1.Items.AddObject('Bitmap4: Key', TheBitmap4);
  151.   ComboBox1.Items.AddObject('Bitmap5: Tools', TheBitmap5);
  152.   ListBox1.Items.AddObject('Bitmap1: Globe', TheBitmap1);
  153.   ListBox1.Items.AddObject('Bitmap2: Video', TheBitmap2);
  154.   ListBox1.Items.AddObject('Bitmap3: Gears', TheBitmap3);
  155.   ListBox1.Items.AddObject('Bitmap4: Key', TheBitmap4);
  156.   ListBox1.Items.AddObject('Bitmap5: Tools', TheBitmap5);
  157.  
  158. end;
  159.  
  160. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  161. begin
  162.   TheBitmap1.Free;
  163.   TheBitmap2.Free;
  164.   TheBitmap3.Free;
  165.   TheBitmap4.Free;
  166.   TheBitmap5.Free;
  167. end;
  168.  
  169. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  170.   Rect: TRect; State: TOwnerDrawState);
  171. var
  172.   Bitmap: TBitmap;
  173.   Offset: Integer;
  174. begin
  175.   with (Control as TComboBox).Canvas do
  176.   begin
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.   PRODUCT  :  Delphi                                 NUMBER  :  2793
  191.   VERSION  :  All
  192.        OS  :  Windows
  193.      DATE  :  May 17, 1995                             PAGE  :  4/6
  194.  
  195.     TITLE  :  Adding Graphics in Your Listboxes and Comboboxes
  196.  
  197.  
  198.  
  199.  
  200.     FillRect(Rect);
  201.     Bitmap := TBitmap(ComboBox1.Items.Objects[Index]);
  202.     if Bitmap <> nil then
  203.     begin
  204.       BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  205.                 Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  206.                 Bitmap.Height), clRed);
  207.       Offset := Bitmap.width + 8;
  208.     end;
  209.     { display the text }
  210.     TextOut(Rect.Left + Offset, Rect.Top, Combobox1.Items[Index])
  211.   end;
  212. end;
  213.  
  214. procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index:
  215.                                       Integer; var Height: Integer);
  216. begin
  217.   height:= 20;
  218. end;
  219.  
  220. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  221.   Rect: TRect; State: TOwnerDrawState);
  222. var
  223.   Bitmap: TBitmap;
  224.   Offset: Integer;
  225. begin
  226.   with (Control as TListBox).Canvas do
  227.   begin
  228.     FillRect(Rect);
  229.     Bitmap := TBitmap(ListBox1.Items.Objects[Index]);
  230.     if Bitmap <> nil then
  231.     begin
  232.       BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  233.                 Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  234.                 Bitmap.Height), clRed);
  235.       Offset := Bitmap.width + 8;
  236.     end;
  237.     { display the text }
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.   PRODUCT  :  Delphi                                 NUMBER  :  2793
  252.   VERSION  :  All
  253.        OS  :  Windows
  254.      DATE  :  May 17, 1995                             PAGE  :  5/6
  255.  
  256.     TITLE  :  Adding Graphics in Your Listboxes and Comboboxes
  257.  
  258.  
  259.  
  260.  
  261.     TextOut(Rect.Left + Offset, Rect.Top, Listbox1.Items[Index])
  262.   end;
  263. end;
  264.  
  265. procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  266.   var Height: Integer);
  267. begin
  268.   height:= 20;
  269. end;
  270.  
  271. end.
  272. {END OWNERDRW.PAS}
  273.  
  274. {START OWNERDRW.DFM}
  275. object Form1: TForm1
  276.   Left = 211
  277.   Top = 155
  278.   Width = 435
  279.   Height = 300
  280.   Caption = 'Form1'
  281.   Font.Color = clWindowText
  282.   Font.Height = -13
  283.   Font.Name = 'System'
  284.   Font.Style = []
  285.   PixelsPerInch = 96
  286.   OnClose = FormClose
  287.   OnCreate = FormCreate
  288.   TextHeight = 16
  289.   object ComboBox1: TComboBox
  290.     Left = 26
  291.     Top = 30
  292.     Width = 165
  293.     Height = 22
  294.     Style = csOwnerDrawVariable
  295.     ItemHeight = 16
  296.     TabOrder = 0
  297.     OnDrawItem = ComboBox1DrawItem
  298.     OnMeasureItem = ComboBox1MeasureItem
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.   PRODUCT  :  Delphi                                 NUMBER  :  2793
  313.   VERSION  :  All
  314.        OS  :  Windows
  315.      DATE  :  May 17, 1995                             PAGE  :  6/6
  316.  
  317.     TITLE  :  Adding Graphics in Your Listboxes and Comboboxes
  318.  
  319.  
  320.  
  321.  
  322.   end
  323.   object ListBox1: TListBox
  324.     Left = 216
  325.     Top = 28
  326.     Width = 151
  327.     Height = 167
  328.     ItemHeight = 16
  329.     Style = lbOwnerDrawVariable
  330.     TabOrder = 1
  331.     OnDrawItem = ListBox1DrawItem
  332.     OnMeasureItem = ListBox1MeasureItem
  333.   end
  334. end
  335. {END OWNERDRW.DFM}
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362. DISCLAIMER: You have the right to use this technical information
  363. subject to the terms of the No-Nonsense License Statement that
  364. you received with the Borland product to which this information
  365. pertains.
  366.